/*
     * drag 1.0
     * create by tony@jentian.com
     * date 2015-08-18
     * 拖动滑块
     */
(function($){
    $.fn.drag = function(options){
        var x, drag = this, isMove = false, defaults = {
        };
        var options = $.extend(defaults, options);
        //添加背景，文字，滑块
        var html = '<div class="drag_bg"></div>'+
            '<div id="thedrag" class="drag_text" onselectstart="return false;" unselectable="on">拖动滑块验证</div>'+
            '<div class="handler handler_bg"></div>';
        this.append(html);

        var handler = drag.find('.handler');
        var drag_bg = drag.find('.drag_bg');
        var text = drag.find('.drag_text');
        var maxWidth = drag.width() - handler.width();  //能滑动的最大间距

        //鼠标按下时候的x轴的位置
        handler.mousedown(function(e){
            isMove = true;
            x = e.pageX - parseInt(handler.css('left'), 10);
        });
        //鼠标指针在上下文移动时，移动距离大于0小于最大间距，滑块x轴位置等于鼠标移动距离
        $(document).mousemove(function(e){
            var _x = e.pageX - x;
            if(isMove){
                if(_x > 0 && _x <= maxWidth){
                    handler.css({'left': _x});
                    drag_bg.css({'width': _x});
                }else if(_x > maxWidth){  //鼠标指针移动距离达到最大时清空事件
                    var emailFlag=0;

                    var email = $("#findemail").val();
                    email = '{"result":"'+email+'"}';
                    $.ajax({
                        async: false,
                        data: email,
                        type: "post",
                        url: serverIpAddress+"User/EmailVerify",
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            email = data.result;
                        }
                    });
                    if(email === "fail")
                        emailFlag = 0;
                    else
                        emailFlag = 1;

                    var reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                    if(reg.test($("#findemail").val()) && $("#changedpassword").val()!="" && emailFlag === 1) {
                        dragOk();
                    }
                    else if($("#changedpassword").val()=="") {
                        alert("验证失败，请输入修改后的密码");
                    }
                    else if(emailFlag===0) {
                        isMove=false;
                        handler.css({'left': 0});
                        drag_bg.css({'width': 0});
                        alert("验证失败，该账号尚未注册");
                    }
                    else {
                        isMove=false;
                        handler.css({'left': 0});
                        drag_bg.css({'width': 0});
                        alert("验证失败，请输入正确的邮箱格式");
                    }
                }
            }
        }).mouseup(function(e){
            isMove = false;
            var _x = e.pageX - x;
            if(_x < maxWidth){ //鼠标松开时，如果没有达到最大距离位置，滑块就返回初始位置
                handler.css({'left': 0});
                drag_bg.css({'width': 0});
            }
        });
        //请求服务器，发送邮件找回密码
        function dragOk(){
            isMove=false;
            handler.css({'left': 0});
            drag_bg.css({'width': 0});
            var email = $("#findemail").val();
            var password = $("#changedpassword").val();
            email = '{"email":"'+email+'","password":"'+password+'"}';
            //把需要找回的账号信息和当前时间存入localstorage
            localStorage.setItem("forgotUser",email);
            localStorage.setItem("sendEmailStartTime",new Date().getTime().toString());
            alert("验证通过！系统已向该邮箱发送信息，请确认是本人操作。");
            // 设置当前剩余的时间，min是分钟,sec是秒，Interval是定时器，这三个都是login.html里的全局变量
            var dragOkSoon = 300000 - (new Date().getTime()-Number(localStorage.getItem("sendEmailStartTime")));
            min = Math.floor(dragOkSoon/60000);
            sec = Math.floor((dragOkSoon - min*60000)/1000);
            // 然后开始定时执行mytime方法倒计时
            Interval=setInterval("mytime()",1000);
            $("#next1").hide();
            $("#next2").show();
            $.ajax({
                async: true,
                data: email,
                type: "post",
                url: serverIpAddress+"User/SendEmail",
                contentType: "application/json;charset=utf-8",
                dataType: "json"
            });
        }
    };
})(jQuery);

//找回密码确认keyWord是否正确，正确则找回成功
function SubmitChangePassword()
{
    var keyWord = $("#keyWord").val();
    let postData = {"forgotUser":localStorage.getItem("forgotUser"),"keyWord":keyWord};
    $.ajax({
        async: false,
        data: JSON.stringify(postData),
        type: "post",
        url: serverIpAddress+"User/SubmitChangePassword",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            keyWord = data.result;
        }
    });
    if(keyWord === "success") {
        //如果找回成功，把存入的localstorage删除
        localStorage.removeItem("forgotUser");
        localStorage.removeItem("sendEmailStartTime");
        alert("更改成功");
        $("#exampleModalCenter3").slideUp(300,function(){
            $("#shadow").hide();
            $("#next2").hide();
            $("#next1").show();
        });
    }
    else {
        alert("更改失败,密钥错误");
        $("#exampleModalCenter3").slideUp(300,function(){
            $("#shadow").hide();
            $("#next2").hide();
            $("#next1").show();
        });
    }
}


